<?php
class heroController extends appController
{
	public function index()
	{
		//$_SESSION['wxid'] = 23498;
        $this->check_member();
		$m = Model("hero");
		//微信分享参数构建
		$jsdk = $m ->get_jsdk();
			
		$this->assign("jsdk", $jsdk);
			
		$this->assign_global($config);
	
		$this->display("index.html");
	
	}
	
	public function clear()
	{
		unset($_SESSION['wxid']);
	}
	
	public function apply()
	{
		$m = Model("hero");
		if(!intval($_SESSION['wxid'])>0){
			$this->showMsg(array("error"=>1,"content"=>"请刷新后重新登录填写资料"));
			exit();
		}
		if($m->isApply($_SESSION['wxid'])>10){//已报名
			$this->showMsg(array("error"=>1,"content"=>"您已报名提交信息超过10次，无法再次提交"));
			exit();
		}
		$info['name'] = trim(yf_empty($_REQUEST['name'],"姓名不可为空"));
		$info['age'] = intval(yf_empty($_REQUEST['age'],"年龄不可为空"));
		$info['sex'] = trim(yf_empty($_REQUEST['sex'],"性别不可为空"));
		if($info['sex']!="男"&&$info['sex']!="女"){
		    $this->showMsg(array("error"=>1,"content"=>"性别请填写“男”或“女”"));
		    exit();
		}
		$info['phone'] = trim(yf_empty($_REQUEST['phone'],"联系电话不可为空"));
		if(hy_check($info['phone'],"m") == '#@_error')
   	    {
   	        $this->showMsg(array("error"=>1,"content"=>"请输入正确的手机号码"));
   	        exit();
   	    }
		$info['specialty'] = strip_tags(trim(yf_empty($_REQUEST['specialty'],"请填写您的武术特长")));
		$info['experience'] = strip_tags(trim(yf_empty($_REQUEST['experience'],"请填写您的演艺经历，如无则填写“无”")));
// 		$info['img_url'] = trim(yf_empty($_REQUEST['img_url'],"请上传图片"));
		$medias = yf_empty($_REQUEST['media'], "请上传至少一张图片。");
		
		$img = $this->getPic($medias);
		
		$arr = array();
		
		if(!is_array($img))
		{
		    $arr[] = $img;
		}
		else
		{
		    $arr = $img;
		}
		
		$info['img_url'] = json_encode($arr);
		$info['wxid'] = intval($_SESSION['wxid']);
		$result = $m->insert($info,"hero_info");
		if($result){
			$this->showMsg(array("error"=>0,"content"=>"提交成功！"));
		}else{
			$this->showMsg(array("error"=>1,"content"=>"提交失败"));
		}
	}
	
	public function upload_img()
	{
		$res = array('error' => 1, 'content' => '');
		$media_id = yf_empty($_POST['img'], "error");
		$token = Model('suipai')->getAccessToken();
		$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
		$open=fopen("log.txt","a" );
		fwrite($open,$url."\r\n");
		fclose($open);
		$img = yf_time().".jpg";
		$img = $this->GrabImage($url, $img);
		$temp = str_replace(ROOT_PATH, "/", $img);
		$html = "<input type=\"hidden\" id=\"pic_url\" name=\"img_url\" value=\"$temp\">";
		$url = "<img src=\"$temp\"  class=\"pre-img\"  />";
		$res = array('error' => 0, 'content' => $html , 'url' =>$url ,'img'=>'http://g4.airefp.com/ws/'.$temp);
		$this->showMsg($res);
	}
	
	public function getPic($medias)
	{
	
	    $token = Model('suipai')->getAccessToken();
	
	    $pictures = array();
	
	    if(!is_array($medias))
	    {
	        $temp = $medias;
	        $medias = array();
	        $medias[] = $temp;
	    }
	     
	    foreach ($medias as $k => $v)
	    {
	        $media_id = $v;
	        $url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
// 	        $open=fopen(ROOT_PATH."/Qiaqia/Log/imgerr.txt","a" );
// 	        fwrite($open,$url."\r\n");
// 	        fclose($open);
	        
	        $img = $k.rand(0, 100).time().".jpg";
	
	        $img = $this->GrabImage($url, $img);
	
	        $temp = str_replace(ROOT_PATH, "/", $img);
            $pictures[] = $temp;
	
	
	    }
	    return $pictures;
	
	
	}
	public function GrabImage($url, $filename = "") {
		if($url == ""):return false;endif;
		if($filename == "") {
			$ext = strrchr($url, ".");
			if($ext != ".gif" && $ext != ".jpg"):return false;endif;
			$filename = date("YmdHis").$ext;
		}
		ob_start();
		readfile($url);
		$img = ob_get_contents();
		ob_end_clean();
		$size = strlen($img);
		$file = 'Uploads/img/'.date('Ym');
		if(!is_dir($file))
		{
			mkdir($file);
		}
		$file = $file."/".$filename;
		$fp2 = @fopen($file, "a");
		fwrite($fp2, $img);
		fclose($fp2);
		return $file;
	}
	
}